//算法课程第一次oj 第一题
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <math.h>
#include <string.h>
int mypow(int a, int b){
    //求 a^b % 1337
    // 如 a^4 % 1337
    // a*a*a*a % 1337 = a^3%1337 * a%1337 %1337
    // a*a*a % 1337 = a^2%1337 * a%1337 % 1337
    //a^2 % 1337 = a%1337 * a%1337 % 1337
    if (b == 0)
        return 1;
    if (b == 1)
        return a;
    if (b == 2)
    {
        return (int)pow((a%1337),2) % 1337;
    }
    int r = a % 1337;
    if (b % 2 == 1)
        return r * mypow(a, b - 1) % 1337;
    else{
        int sub = mypow(a, b / 2);
        return (sub * sub) % 1337;
    }
}
int super_pow(int a, std::vector<int> &b){

    if (b.size() == 0)
        return 1;
    int last = b[b.size() - 1];
    b.pop_back();

    int sub1 = mypow(a, last) % 1337;
    int sub2 = mypow( super_pow(a,b) , 10 ) % 1337;
    return  (sub1 *sub2) % 1337;
}

int main(){
    using namespace std;
    //重要的俩点：
    // (a*b) % k = (a%k) * (b%k) % k
    // a^[1,2,3,4] = a^4 * a^[1,2,3,0] = a^4 * [a^[1,2,3]]^10，这样划分成一个小规模的子问题，递归实现
    int a ;
    vector<int> barr;
    string str, aa;
    getline(cin, aa);
    a = stoi(aa);
    getline(cin, str);
    //把字符串转化为数组
    // [112,21,3111,41,51]
    vector<char> b(str.length());
    b.assign(str.begin(), str.end());
    b.erase(b.begin());  //去除vector第一位
    b.pop_back();  //去除vector最后一个元素
    string temp = "";
    for (int i = 0; i < b.size(); i++){
        if (b[i] != ',' && b[i] != ' '){
            temp += b[i];
        }else{
            if (temp != ""){
                barr.push_back(stoi(temp));
                temp = "";
            }
        }
    }
    barr.push_back(stoi(temp));
    cout << super_pow(a, barr) << endl;
    return 0;
}